﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Backend;
using Data_Layer;

namespace WCFService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service.svc or Service.svc.cs at the Solution Explorer and start debugging.
    public class Service : IRESTService, ISOAPService
    {

        private DBBooking dbbooking = new DBBooking();
        private DBUsers dbuser = new DBUsers();
        private DBUsersBooking dbusersbooking = new DBUsersBooking();

        private Booking booking = new Booking();
        private Users user = new Users();
        private UsersBooking usersbooking = new UsersBooking();


        //BOOKING Methods      

        public int createBooking(string plateNO, string bookedTime)
        {
            Random random = new Random();
            DateTime startTime = DateTime.Now;
            string startTime_ = startTime.ToString();

            booking.BookingNO = random.Next(10000);
            booking.PlateNO = plateNO;
            booking.StartTime = startTime_;
            booking.Status = "active";
            booking.TimeBooked_hours = Convert.ToInt32(bookedTime);

            dbbooking.createBooking(booking);

            return booking.BookingNO;
        }

        public string deleteBooking(string bookingNO)
        {
            int booking_number = Convert.ToInt32(bookingNO);
            dbbooking.deleteBooking(booking_number);
            return "Success";
        }

        public Booking readBooking(string bookingNO)
        {
            try
            {
                int booking_number = Convert.ToInt32(bookingNO);
                Booking bookingINFO = dbbooking.readBooking(booking_number);
                return bookingINFO;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public string updateBooking(string bookingNO, string bookedTime)
        {
            Random random = new Random();
            DateTime startTime = DateTime.Now;
            string startTime_ = startTime.ToString("yyyy-MM-dd H:mm:ss");

            booking.BookingNO = Convert.ToInt32(bookingNO);
            booking.PlateNO = (dbbooking.readBooking(Convert.ToInt32(bookingNO)).PlateNO);
            booking.StartTime = startTime_;
            booking.Status = "active";
            booking.TimeBooked_hours = Convert.ToInt32(bookedTime);

            dbbooking.updateBooking(booking);

            return "Success";
        }

        public Booking[] getallBookings()
        {
            List<Booking> bookings = dbbooking.getallBookings();
            return bookings.ToArray();

        }

        //USER Methods

        public void createUser(string username, string password, int balance)
        {
            user.UserName = username;
            user.PassWord = password;
            user.Balance = balance;
            dbuser.createUser(user);
        }

        public void deleteUser(string username)
        {
            string userName = username;
            dbuser.deleteUser(userName);
        }

        public void updateUser(string username, string password, int balance)
        {
            user.UserName = username;
            user.PassWord = password;
            user.Balance = balance;
            dbuser.updateUser(user);
        }

        public void updateUserbalance(string username, int balance)
        {
            user.UserName = username;            
            user.Balance = balance;
            dbuser.updateUserbalance(user.UserName, user.Balance);
        }
        public Users readUser(string username)
        {
            try
            {
                string userName = username;
                Users user = dbuser.readUser(userName);
                return user;
            }
            catch (Exception e)
            {
                return null;
            }

        }

        public List<Users> getallUsers()
        {
            List<Users> users = dbuser.getallUsers();
            return users;
        }

        //USERSBOOKING Methods

        public void createUsersBooking(string username, int bookingNO)
        {
            usersbooking.UserName = username;
            usersbooking.BookingNO = bookingNO;
            dbusersbooking.createUsersBooking(usersbooking);
        }

        public void deleteUsersBooking(string username)
        {
            string userNAME = username;
            dbusersbooking.deleteUsersBooking(userNAME);
        }

        public UsersBooking readUsersBooking(string username)
        {
            string userName = username;
            UsersBooking usersbooking = dbusersbooking.readUsersBooking(userName);
            return usersbooking;
        }

        public List<UsersBooking> getallUsersBooking()
        {
            List<UsersBooking> usersbooking = dbusersbooking.getallUsersBooking();
            return usersbooking;
        }

        public List<UsersBooking> getallUsersBookingsbyUsername(string UserName)
        {
            List<UsersBooking> allbookingsbyUsername = dbusersbooking.getallUsersBookingbyUsername(UserName);
            return allbookingsbyUsername;
        }

        public List<Booking> getallBookingsbybookingNO(int bookingnumber)
        {
            List<Booking> bookings = dbbooking.getallBookingsbybookingNO(bookingnumber);
            return bookings;
        }
    }
}
